<?php
/**
* 支付单插件
*
* @author chenping<chenping@shopex.cn>
* @version $Id: payment.php 2013-3-12 17:23Z
*/
class erpapi_shop_response_plugins_order_payment extends erpapi_shop_response_plugins_order_abstract
{

    static private $__codPartPay = array('yihaodian','dangdang');

    public function convert(erpapi_shop_response_abstract $platform)
    {
        $paymentsdf = array();
        # 支付单结构
        $payment_list = isset($platform->_ordersdf['payments']) ? $platform->_ordersdf['payments'] : array($platform->_ordersdf['payment_detail']);

        $paymentObj = app::get('ome')->model('payments');
        # 支付单存在且订单是已支付创建支付单(或者一号店订单，货到付款，部分支付的,创建支付单)
        if (($payment_list && $platform->_ordersdf['pay_status'] == '1') || 
            ('true' == $platform->_ordersdf['shipping']['is_cod'] 
                && in_array($platform->__channelObj->channel['node_type'], self::$__codPartPay) 
                && $platform->_ordersdf['pay_status'] == '3')) {

            $payment_cfg = $platform->get_payment($platform->_ordersdf['pay_bn'],$platform->__channelObj->channel['node_type']);


            $obj_pam    = app::get('pam')->model('account');
            foreach ($payment_list as $payment) {
                $payment['op_name'] = trim($payment['op_name']);

                $account_info = array();
                if($payment['op_name']){
                    #查询支付单上的操作人,是否能在系统找到
                    $account_info = $obj_pam->getList('account_id',array('login_name' => $payment['op_name']));
                    
                    if($account_info[0]) $payment['op_id'] =  $account_info[0]['account_id']; 
                }

                if(!$payment['pay_time']) $payment['pay_time'] = time();

                $t_begin = $t_end = kernel::single('ome_func')->date2time($payment['pay_time']);
                
                if ($payment['trade_no'] === 'null') unset($payment['trade_no']);

                $paymentsdf[] = array(
                    'payment_bn'    => $payment['trade_no'],
                    'shop_id'       => $platform->__channelObj->channel['shop_id'],
                    'order_id'      => null,
                    'account'       => $payment['account'],
                    'bank'          => $payment['bank'],
                    'pay_account'   => $payment['pay_account'],
                    'currency'       => 'CNY',
                    'money'         => (float)$payment['money'],
                    'paycost'       => $payment['paycost'],
                    'cur_money'     => (float)$payment['money'],
                    'pay_type'      => $payment_cfg['pay_type'],
                    'payment'       => $payment_cfg['id'],
                    'pay_bn'        => $payment_cfg['pay_bn'],
                    'paymethod'     => $payment['paymethod'],
                    't_begin'       => $t_begin ? $t_begin : time(),
                    't_end'         => $t_end ? $t_end : time(),
                    'download_time' => time(),
                    'status'        => 'succ',
                    'trade_no'      => $payment['trade_no'],
                    'memo'          => $payment['memo'],
                    'op_id'         => $payment['op_id'] ? $payment['op_id'] : '',
                    'op_name'       => $payment['op_name'] ? $payment['op_name'] : $platform->__channelObj->channel['node_type'],
                );
            }
        }

        // 更新的时候
        if ($platform->_tgOrder) {
            $tgPayments = $paymentObj->getList('payment_bn',array('order_id'=>$platform->_tgOrder['order_id']));
            $paymentbns = $tgPayments ? array_map('current',$tgPayments) : array();

            foreach ($paymentsdf as $key => $value) {
                if (in_array($value['payment_bn'],$paymentbns)) {
                    unset($paymentsdf[$key]);continue;
                }
            }
        }


        return $paymentsdf;
    }

    /**
     * @param Array $params
     * 
     * @return void
     * @author 
     **/
    public function postCreate($order_id,$payments)
    {
        $paymentObj = app::get('ome')->model('payments');
        $bank       = app::get('ome')->model('bank_account');
        foreach ($payments as $key => $value) {
            $payments[$key]['order_id']   = $order_id;
            $payments[$key]['payment_bn'] = $value['payment_bn'] ? $value['payment_bn'] : $paymentObj->gen_id();

            $bankAccount = array('bank'=>$value['bank'], 'account'=>$value['account']);
            if(!$bank->dump($bankAccount)) {
                $bank->save($bankAccount);
            }
        }
        
        $sql = ome_func::get_insert_sql($paymentObj,$payments);

        kernel::database()->exec($sql);
    }

    /**
     * @param Array $params
     * 
     * @return void
     * @author 
     **/
    public function postUpdate($order_id,$payments)
    {
        $shop_id = $payments[0]['shop_id'];

        $shop = $this->getShop($shop_id);

        $paymentObj = app::get('ome')->model('payments');

        if (in_array($shop['node_type'],array('ecshop_b2c','bbc'))) {
            $paymentObj->delete(array('order_id'=>$order_id));
        } 

        foreach ($payments as $key => $value) {

            $payments[$key]['order_id']   = $order_id;

            if (in_array($shop['node_type'],array('ecshop_b2c','bbc'))) {
                $payments[$key]['payment_bn'] = $value['payment_bn'] ? $value['payment_bn'] : $paymentObj->gen_id();
            }
        }

        if ($payments) { 
            $sql = ome_func::get_insert_sql($paymentObj,$payments);

            kernel::database()->exec($sql);

            $logModel = app::get('ome')->model('operation_log');
            $logModel->write_log('order_pay@ome',$order_id,'支付单添加');      
        }
    }
}